package me.youline.dataServer.service.front;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import me.youline.dataServer.controller.dto.PubMsgBaseFrontDTO;
import me.youline.dataServer.controller.dto.PubMsgDetailFrontDTO;
import me.youline.dataServer.dao.ArContentEntityDAO;
import me.youline.dataServer.entity.PublicMessage;
import me.youline.dataServer.entity.PublicMessageIndex;
import me.youline.dataServer.service.MsgPraiseRecordService;
import me.youline.dataServer.service.SocialRelationService;
import me.youline.dataServer.service.support.file.FileLocatorManager;

import org.perf4j.aop.Profiled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 消息前端展示实体转换业务类
 * 
 * @author linhan
 *
 */
@Component
public class PublicMessageDTOAdapter {

	private static Logger logger = LoggerFactory
			.getLogger(PublicMessageDTOAdapter.class);

	@Autowired
	private UserInfoDTOAdapter userInfoAssembler;
	
	@Autowired
	private SocialRelationDTOAdapter socialRelationDTOAdapter;

	@Autowired
	private FileLocatorManager locateManager;

	@Autowired
	private ArContentEntityDAO arContentEntityDAO;

	@Autowired
	private MsgPraiseRecordService praiseRecordService;
	
	@Autowired
	private SocialRelationService socialRelationService;

	/**
	 * 根据公告领域对象获取公共消息DTO
	 * 
	 * @param message
	 * @return
	 */
	@Profiled(tag = "front-批量获取消息展示实体数据")
	public List<PubMsgDetailFrontDTO> getPubMsgDetailFrontDTOs(Long viewUserId,
			List<PublicMessage> message) {
		if (message == null || message.isEmpty()) {
			return Collections.emptyList();
		}
		List<PubMsgDetailFrontDTO> list = new ArrayList<PubMsgDetailFrontDTO>();
		for (PublicMessage m : message) {
			try {
				list.add(this.getPubMsgDetailDTO(viewUserId, m));
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
		}
		return list;
	}
	
	public List<PubMsgBaseFrontDTO> getPubMsgBaseFrontDTOs(List<PublicMessageIndex> indexs){
		if (indexs == null || indexs.isEmpty()) {
			return Collections.emptyList();
		}
		List<PubMsgBaseFrontDTO> list = new ArrayList<PubMsgBaseFrontDTO>();
		for (PublicMessageIndex m : indexs) {
			try {
				list.add(this.getPubMsgBaseDTO(m));
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
		}
		return list;
		
	}

	/**
	 * 根据公告领域对象获取公共消息DTO
	 * 
	 * @param message
	 * @return
	 */
	@Profiled(tag = "front-获取消息前端展示实体")
	public PubMsgDetailFrontDTO getPubMsgDetailDTO(Long viewUserId,
			PublicMessage message) {
		PubMsgDetailFrontDTO dto = new PubMsgDetailFrontDTO();
		if (message.getMessageRecorder() == null) {
			dto.setPraiseNum(0);
			dto.setDisplayPicUrl("");
		} else {
			dto.setPraiseNum(message.getMessageRecorder().getPraiseNum());
			dto.setDisplayPicUrl(locateManager.getDisplayPicFileLocator()
					.getFileWebUrl(message.getDisplayPicUrl()));
		}
		dto.setRelStatus(socialRelationDTOAdapter.getRelationStatusDTO(viewUserId, message.getCreatorId()));
		dto.setMessageId(message.getId());
		dto.setCreator(userInfoAssembler.getSimpleInfoDTO(message
				.getCreatorId()));
		dto.setCreateTime(message.getPublishTime());
		dto.setIsPraised(!praiseRecordService.isAbleToPraiseMsg(message.getId(), viewUserId));
		return dto;
	}
	
	protected PubMsgBaseFrontDTO  getPubMsgBaseDTO(PublicMessageIndex index){
		PubMsgBaseFrontDTO dto = new PubMsgBaseFrontDTO();
		dto.setMessageId(index.getMsgId());
		dto.setDisplayPicUrl(locateManager.getDisplayPicFileLocator().getFileWebUrl(index.getDisplayPicUrl()));
		dto.setCreatorId(index.getCreatorId());
		dto.setCreateTime(index.getCreateTime());
		return dto;
	}

	
}
